package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.Advice;
import com.example.smartcity.Mappers.AdviceMapper;
import com.example.smartcity.Mappers.UserMapper;
import com.example.smartcity.Service.AdviceService;
import com.example.smartcity.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.hibernate.mapping.Map;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("AdviceService")
public class AdviceServiceImpl implements AdviceService {
    public String AddAdvice(Advice advice){
        JSONObject map = new JSONObject();
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdviceMapper adviceMapper = sqlSession.getMapper(AdviceMapper.class);
        try {
            adviceMapper.InsertAdvice(advice);
            sqlSession.commit();
            System.out.println("添加建议成功");
            map.put("code",200);
            map.put("msg","添加建议成功");

        }catch (Exception e){
            System.out.println(e);
            System.out.println("添加建议失败");
            e.printStackTrace();
            map.put("code",400);
            map.put("msg","添加建议失败");
        }finally {
            sqlSession.close();
        }
        return JSONObject.toJSONString(map);
    }
    //回复建议
    public String ReplyAdvice(String reply , int id){
        JSONObject map = new JSONObject();
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdviceMapper adviceMapper = sqlSession.getMapper(AdviceMapper.class);
        try {
            adviceMapper.ReplyAdvice(reply,id);
            sqlSession.commit();
            System.out.println("回复建议成功");
            map.put("code",200);
            map.put("msg","回复建议成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("回复建议失败");
            map.put("code",400);
            map.put("msg","回复建议失败");
        }finally {
            sqlSession.close();
        }
        return JSONObject.toJSONString(map);
    }
    public List<Advice> findAll(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdviceMapper adviceMapper = sqlSession.getMapper(AdviceMapper.class);
        List<Advice> list = adviceMapper.findAll();
        sqlSession.close();
        return list;
    }




}
